package com.qx.pethome.util;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**
 * JDBC 工具类
 */
public class JDBCUtils {
    // 创建链接
    public static Connection getConnection() throws Exception {
        // druid操作
        // ① 读取配置信息
        // 注意：路径前缀/，表示的资源路径
        InputStream is = JDBCUtils.class.getResourceAsStream("/db.properties");
        Properties properties = new Properties();
        properties.load(is);
        // ② 创建数据库连接池（数据源）
        DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
        // ③ 获得连接
        Connection connection = dataSource.getConnection();
        return connection;
    }

    // 关闭资源
    public static void close(Connection conn, Statement smt) throws SQLException {
        smt.close();
        conn.close();
    }

    public static void close(Connection conn, Statement smt, ResultSet rs) throws SQLException {
        rs.close();
        smt.close();
        conn.close();
    }


}
